home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / MovieScroll-c / MovieScroll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-04  |  7.0 KB  |  303 lines  |  [TEXT/KAHL]

  1. //• --------------------------------------------------------------- •//
  2. //• A public domain demo of "movie" type scrolling,    courtesy of        •//
  3. //• Kenneth A. Long (kenlong@netcom.com).                            •//
  4. //• An itty bitty bytes™ production, for the benefit of anyone        •//
  5. //• who can use it.                                                    •//
  6. //• This is basically just "Bullseye" with some things dumped and    •//
  7. //• two routines added, and a resource file to back the additions.    •//
  8.  
  9. //• If you want the text drawn over black, like in the movies,         •//
  10. //• then some other "girations" are necessary.  This just shows     •//
  11. //• the scrolling, with style.                                        •//
  12.  
  13. //• Enjoy!                                                            •//
  14.  
  15. //• --------------------------------------------------------------- •//
  16.  
  17. //• BullShell.c
  18.  
  19. void InitMacintosh (void);
  20. void SetUpMenus (void);
  21. void HandleEvent (void);
  22. void HandleMouseDown (EventRecord *theEvent);
  23. void AdjustMenus (void);
  24. static void enable (MenuHandle menu, short item, short ok);
  25. void HandleMenu (long mSelect);
  26. void main (void);
  27. pascal void OutlineDefault (WindowPtr myDialog, short itemNo);
  28. void DoAbout (void);
  29.  
  30. #define aboutID 128
  31. TEHandle textHand;
  32.  
  33. MenuHandle    appleMenu, fileMenu;
  34.  
  35. enum    {
  36.     appleID = 1,
  37.     fileID
  38. };
  39.  
  40. enum    {
  41.     quitItem = 1
  42. };
  43.  
  44. WindowPtr    shell_window;
  45.  
  46. void InitMacintosh(void)
  47. {
  48.     MaxApplZone();
  49.     
  50.     InitGraf(&qd.thePort);
  51.     InitFonts();
  52.     FlushEvents(everyEvent, 0);
  53.     InitWindows();
  54.     InitMenus();
  55.     TEInit();
  56.     InitDialogs(0L);
  57.     InitCursor();
  58.  
  59. }
  60.  
  61. void SetUpMenus (void)
  62. {
  63.     InsertMenu (appleMenu = NewMenu (appleID, "\p\024"), 0);
  64.     InsertMenu (fileMenu = NewMenu (fileID, "\pFile"), 0);
  65.     DrawMenuBar ();
  66.     AppendMenu (appleMenu, "\pAbout item");
  67.     AddResMenu (appleMenu, 'DRVR');
  68.     AppendMenu (fileMenu, "\pQuit/Q");
  69. }
  70.  
  71. void HandleEvent(void)
  72. {
  73.     int            ok;
  74.     EventRecord    theEvent;
  75.  
  76.     HiliteMenu(0);
  77.     SystemTask ();        //• Handle desk accessories.
  78.     
  79.     ok = GetNextEvent (everyEvent, &theEvent);
  80.     if (ok)
  81.         switch (theEvent.what)
  82.         {
  83.             case mouseDown:
  84.                 HandleMouseDown(&theEvent);
  85.             break;
  86.             
  87.             case keyDown: 
  88.             case autoKey:
  89.             if ((theEvent.modifiers & cmdKey) != 0)
  90.             {
  91.                    AdjustMenus();
  92.                 HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  93.             }
  94.             break;
  95.             
  96.             case updateEvt:
  97.                 BeginUpdate(shell_window);
  98.                 EndUpdate(shell_window);
  99.             break;
  100.             
  101.             case activateEvt:
  102.                 InvalRect(&shell_window->portRect);
  103.             break;
  104.         }
  105. }
  106.  
  107. void HandleMouseDown (EventRecord    *theEvent)
  108. {
  109.     WindowPtr    theWindow;
  110.     int            windowCode = FindWindow (theEvent->where, &theWindow);
  111.     
  112.     switch (windowCode)
  113.     {
  114.         case inSysWindow: 
  115.             SystemClick (theEvent, theWindow);
  116.         break;
  117.         
  118.         case inMenuBar:
  119.               AdjustMenus();        //• Left over - not really needed.
  120.             HandleMenu(MenuSelect(theEvent->where));
  121.         break;
  122.             
  123.         case inContent:
  124.             if (theWindow == shell_window)
  125.             {
  126.                 if (theWindow != FrontWindow())
  127.                     SelectWindow(shell_window);
  128.                 else
  129.                     InvalRect(&shell_window->portRect);
  130.             }
  131.         break;
  132.           
  133.         case inGoAway:
  134.         if (theWindow == shell_window && 
  135.             TrackGoAway(shell_window, theEvent->where))
  136.             HideWindow(shell_window);
  137.         break;
  138.     }
  139. }
  140.  
  141. void AdjustMenus(void)
  142. {
  143.     register WindowPeek wp = (WindowPeek) FrontWindow();
  144.     short kind = wp ? wp->windowKind : 0;
  145.     Boolean DA = kind < 0;
  146.         
  147. }
  148.  
  149.  
  150. static void enable(MenuHandle menu, short item, short ok)
  151. {
  152.     if (ok)
  153.         EnableItem(menu, item);
  154.     else
  155.         DisableItem(menu, item);
  156. }
  157.  
  158. void HandleMenu (long mSelect)
  159. {
  160.     int            menuID = HiWord(mSelect);
  161.     int            menuItem = LoWord(mSelect);
  162.     Str255        name;
  163.     GrafPtr        savePort;
  164.     WindowPeek    frontWindow;
  165.     
  166.     switch (menuID)
  167.     {
  168.         case    appleID:
  169.             if (menuItem == 1)
  170.                 DoAbout ();
  171.             else
  172.                 {
  173.                     GetPort (&savePort);
  174.                     GetItem (appleMenu, menuItem, name);
  175.                     OpenDeskAcc (name);
  176.                     SetPort (savePort);
  177.             }
  178.         break;
  179.     
  180.         case    fileID:
  181.             switch (menuItem)
  182.             {              
  183.                 if (frontWindow->windowKind < 0)
  184.                     CloseDeskAcc(frontWindow->windowKind);
  185.                 else 
  186.                     if ((frontWindow = (WindowPeek) shell_window) != NULL)
  187.                         HideWindow(shell_window);
  188.                   break;
  189.                         
  190.                 case    quitItem:
  191.                     ExitToShell();
  192.                 break;
  193.             }
  194.         break;
  195.     }
  196. }
  197.  
  198.  
  199. void main( void)
  200. {
  201.     InitMacintosh();
  202.     SetUpMenus();
  203.     DoAbout ();
  204.     for (;;)
  205.         HandleEvent();
  206. }
  207.  
  208. pascal void OutlineDefault (WindowPtr myDialog, short itemNo)
  209. {
  210.     short x;
  211.     Rect rectangle;
  212.     Handle handle;
  213.  
  214.     GetDItem((DialogPtr) myDialog, itemNo, &x, &handle, &rectangle);
  215.     PenSize(3, 3);
  216.     InsetRect(&rectangle, -4, -4);
  217.     FrameRoundRect(&rectangle, 16, 16);
  218.     PenSize(1, 1);
  219. }
  220.  
  221. void DoAbout(void)        //• 'About...' dialog box.
  222. {
  223.     #define SCREEN        2        //• Text window useritem.
  224.     #define SHOWTIME    3
  225.     short USERITEM = 7;            //• Used to outline default button.
  226.     short textID = 128;            //• rsrc ID# for text used.
  227.     short styleID = 128;        //• rsrc ID# for styl used.
  228.  
  229.     GrafPtr saveWPtr;            //• Holds previous grafPtr.
  230.     DialogPtr aboutPtr;            //• Pointer to dialog.
  231.     short theItem;                //• Item selected by user.
  232.     Point mLoc;                    //• Mouse location.
  233.     Rect rectangle;
  234.     short x;
  235.     Handle handle;
  236.     Rect txtRect;                //• Used to hold viewRect.
  237.     TEHandle saveTxtHdl;        //• Text handle.
  238.     Boolean finished;            //• Pushed 'The End' button yet?
  239.     StScrpHandle styleHdl;
  240.     OSErr err;
  241.  
  242.     GetPort(&saveWPtr);                //• Save the old port.
  243.     saveTxtHdl = textHand;            //• Save the old text hdl.
  244.  
  245.     //• Get dialog box pointer.
  246.     aboutPtr = GetNewDialog(aboutID, nil, (WindowPtr)-1);    
  247.  
  248.     //• This next makes the static text field font monaco 9.
  249.     SetPort((GrafPtr) aboutPtr);    //• Output to dialog.
  250.     TextSize(9);                    //• Set text size.
  251.     TextFont(monaco);                //• Set text font.
  252.  
  253.     //• Get text window rect.
  254.     GetDItem((DialogPtr) aboutPtr, SCREEN, &x, &handle, &txtRect);    
  255.     InsetRect(&txtRect, 5, 1);        //• Leave margins for text.
  256.  
  257.     //• Create styled TERecord.
  258.     textHand = TEStylNew(&txtRect, &txtRect);
  259.  
  260.     //• Read the TEXT resource.
  261.     handle = GetResource('TEXT', textID);    
  262.     HLock(handle);                    //• Lock handle.
  263.  
  264.     //• Get the style handle.
  265.     styleHdl = (StScrpHandle)(Get1Resource('styl', styleID));    
  266.     TEStylInsert(*handle, SizeResource(handle), styleHdl, textHand);    //• move text into text record.
  267.     TESetJust (1, textHand);
  268.     HUnlock(handle);                    //• Unlock handle.
  269.  
  270.     ShowWindow(aboutPtr);                //• Show dialog box now.
  271.     TEUpdate(&txtRect, textHand);        //• Draw text in viewRect.
  272.     InsetRect(&txtRect, -5, -1);        //• Leave margins for text.
  273.     FrameRect(&txtRect);                //• Draw frame around text.
  274.     OutlineDefault(aboutPtr, USERITEM);
  275.     GetDItem((DialogPtr) aboutPtr, USERITEM, &x, &handle, &rectangle);    //• outline default button.
  276.     SetDItem(aboutPtr, USERITEM, x, (Handle) OutlineDefault, &rectangle);    //• redraw if erased*/
  277.  
  278.     finished = false;                //• Reset flag.
  279.     do {                            //• Repeat until finished.
  280.         ModalDialog(0L, &theItem);    //• Show dialog/get result.
  281.         switch (theItem) {            //• Control hit.
  282.             case 1:
  283.                 finished = true;    //• "The End" button hit / close.
  284.                 break;
  285.             case SHOWTIME:
  286.                 do
  287.                 {
  288.                     //• scroll up a pixel
  289.                     TEScroll(0, -1, textHand);        
  290.                 }while (! Button ());
  291.                 break;
  292.             default:
  293.                 break;                //• None of the above, avoid range error.
  294.         }                            //• End of  case.
  295.     } while (!finished);            //• End of  mainloop.
  296.  
  297.     TEDispose(textHand);            //• Reclaim heap space.
  298.     textHand = saveTxtHdl;            //• Restore global textHand.
  299.     DisposDialog(aboutPtr);            //• Get rid of dialog box.
  300.     SetPort(saveWPtr);                //• Restore the old port.
  301. }
  302.  
  303.